package cn.hd.controller;

import cn.hd.dao.ReallynameDao;
import cn.hd.entity.AuthRealName;
import cn.hd.entity.Reallyname;
import cn.hd.entity.Result;
import cn.hd.entity.User;
import cn.hd.service.ReallynameService;
import cn.hd.util.MailUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@CrossOrigin
public class ReallynameControl {
    @Autowired
    private ReallynameService reallynameService;
    @Autowired
    private ReallynameDao reallynameDao;

    @RequestMapping("/addReallyname")
    public Object addReallyname(Reallyname reallyname){
        int i = reallynameService.addReallyname(reallyname);
        if(i!=0){
            Reallyname selectzxreal = reallynameDao.selectzxreal();
            AuthRealName authRealName=new AuthRealName("实名认证审批流程",2,"人工审核",selectzxreal.getRealname() ,0  );
            int i1 = reallynameDao.addShreal(authRealName);
            if(i1!=0){
                return new Result("200","实名认证审核中",null);
            }else {
                return new Result("400","审核请求失败",null);
            }
        }else{
            return new Result("400","添加失败",null);
        }
    }

    @RequestMapping("/selectByUserIdRealname")
    public Object selectByUserIdRealname(long userid){
        Reallyname reallyname = reallynameService.selectByUserIdRealname(userid);
        if(reallyname==null){
            return new Result("400","查询失败",null);
        }else{
            AuthRealName authRealName = reallynameDao.selectAuthRealByrealname(reallyname.getRealname());
            if(authRealName==null){
                return new Result("400","查询失败",null);
            }else {
                reallyname.setStatus(authRealName.getStatus());
                return new Result("200","查询成功",reallyname);
            }
        }
    }

    @RequestMapping("/addImg")
    public void addImg(String filename, HttpServletResponse response) throws Exception{
        File file=new File("d:/img",filename);
        FileInputStream fin=new FileInputStream(file);

        response.setHeader("Content-Disposition","filename="+filename);
        ServletOutputStream fout=response.getOutputStream();
        IOUtils.copy(fin,fout);
        fout.close();
        fin.close();
    }

    //图片单独上传
    @RequestMapping("/upload")
    public Object upload(MultipartFile file) throws Exception{
        String filename = file.getOriginalFilename();
        if(filename!=null && !"".equals(filename)){
            if(filename.contains("/")){
                filename=filename.substring(filename.lastIndexOf("/")+1);
            }
            filename= UUID.randomUUID().toString().replace("-","")+"-"+filename;
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
            Date date=new Date();
            String s = sdf.format(date);
            File fi=new File("D:\\img",s);
            if(!fi.exists()){
                fi.mkdirs();  //创建文件夹
            }
            file.transferTo(new File(fi,filename));
        }
        return new Result("200",filename,null);
    }

    @RequestMapping("/passAuthRealName")
    public Object passAuthRealName(long authrealid){
        int i=reallynameService.passAuthRealName(authrealid);
        AuthRealName authRealName = reallynameService.selectByIdAuthRealName(authrealid);
        Reallyname reallyname = reallynameService.selectByUname(authRealName.getUname());
        int i1 = reallynameService.updaterole(reallyname.getUserid());
        if (i!=0 && i1!=0){
            return new Result("200","修改成功",null);
        }
        return  new Result("400","修改失败",null);
    }

    @RequestMapping("/failAuthRealName")
    public Object failAuthRealName(long authrealid){
        int i=reallynameService.failAuthRealName(authrealid);
        if (i!=0){
            return new Result("200","修改成功",null);
        }
        return  new Result("400","修改失败",null);
    }

    @RequestMapping("/selectAuthRealName")
    public Object selectAuthRealName(int pagenum,int pagesize,String uname){
        Map map=new HashMap();
        map.put("uname",uname);
        PageInfo<AuthRealName> page = reallynameService.selectAuthRealName(pagenum, map, pagesize);

        return new Result("200","查询成功",page);
    }

    @RequestMapping("/selectByIdAuthRealName")
    public Object selectByIdAuthRealName(long authrealid){
        AuthRealName authRealName = reallynameService.selectByIdAuthRealName(authrealid);
        return new Result("200","查询成功",authRealName);
    }

    @RequestMapping("/yz")
    public Object yz(String email) throws Exception{
        String yzm = "";
        Random rd = new Random();
        while (yzm.length() != 4) {
            String rn = rd.nextInt(10) + "";
            if (yzm.length()==1){
                if ("0".equals(rn)){
                    continue;
                }
            }
            if (yzm.indexOf(rn) == -1){
                yzm += rn;
            }
        }
        MailUtils.aa(email,yzm);
        return new Result("200","发送验证码成功",yzm);
    }

    @RequestMapping("selectByUname")
    public Object selectByUname(String uname){
        Reallyname reallyname = reallynameService.selectByUname(uname);
        if(reallyname==null){
            return new Result("400","查询失败！",null);
        }else{
            return new Result("200","查询成功！",reallyname);
        }
    }

    //根据用户编号修改审核失败状态
    @RequestMapping("/updateRealStatus")
    public Result updateRealStatus(long userid){
        Reallyname reallyname = reallynameService.selectByUserIdRealname(userid);
        int i = reallynameDao.updateRealStatus(reallyname.getRealname());
        if (i!=0){
            return new Result("200","修改成功",null);
        }else{
            return  new Result("400","修改失败",null);
        }
    }

    //根据用户编号删除审核失败的实名认证表中的信息和对应的审核表中的信息
    @RequestMapping("/delRealStatus")
    public Result delRealStatus(long userid){
        Reallyname reallyname = reallynameService.selectByUserIdRealname(userid);
        int i = reallynameDao.deleteRealByRealname(reallyname.getRealname());
        int i1 = reallynameDao.deleteRealByUserid(userid);
        if(i!=0 && i1!=0){
            return new Result("200","删除成功",null);
        }else{
            return new Result("200","删除失败",null);
        }
    }

}
